package ${packageName};

<#list impImports as imports>
import ${imports};
</#list>
/**
 * ${tableName}
 */
public class ${d_tableName}Imp extends ${d_tableName}Cache{
	static ${d_tableName}Imp instance = new ${d_tableName}Imp();
	static final String lOCK_KEY = "${d_tableName}Imp_lock";
	private ${d_tableName}Imp(){
	}
	
	/**
	 * 异步   非数据安全
	 */
	public static ${d_tableName}Imp asyn(){
		return instance;
	}
	
	/**
	 * 同步(加锁)   一直等待   数据安全
	 */
	public static ${d_tableName}Imp sync(){
		SK_Lock.lock(lOCK_KEY);
		return instance;
	}
	
	/**
	 * 同步(加锁)  可设定超时时间   数据安全
	 */
	public static ${d_tableName}Imp sync(long time){
		ReentrantLock lock = null;
		try {
			lock = SK_Lock.tryLock(lOCK_KEY,time,TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return (lock!=null ? instance : null);	
	}
	
	public static void unlock(){
		SK_Lock.unlock(lOCK_KEY);
	}
	
	
	public ${d_tableName} insert(${d_tableName} ${x_tableName}){
		${d_tableName} ${x_tableName}_ = null;
		ReentrantLock lock = SK_Lock.getLock(lOCK_KEY);
		if (lock.isHeldByCurrentThread()) 
			${x_tableName}_ = super.insert(${x_tableName});
		unlock();
		return ${x_tableName}_;
    }
    
    public ${d_tableName} update(${d_tableName} ${x_tableName}){
    	${d_tableName} ${x_tableName}_ = null;
    	ReentrantLock lock = SK_Lock.getLock(lOCK_KEY);
    	if (lock.isHeldByCurrentThread())
			${x_tableName}_ = super.update(${x_tableName});
		unlock();
		return ${x_tableName}_;
    }
    
    public boolean delete(${d_tableName} ${x_tableName}){
    	boolean bool = false;
    	ReentrantLock lock = SK_Lock.getLock(lOCK_KEY);
    	if (lock.isHeldByCurrentThread()) 
			bool = super.delete(${x_tableName});
		unlock();
		return bool;
    }
    
    public ${d_tableName} insertAndFlush(${d_tableName} ${x_tableName}){
    	${d_tableName} ${x_tableName}_ = null;
    	ReentrantLock lock = SK_Lock.getLock(lOCK_KEY);
    	if (lock.isHeldByCurrentThread())
    		${x_tableName}_ = super.insertAndFlush(${x_tableName});
    	unlock();
    	return ${x_tableName}_;
    }
    
    
    
    public ${d_tableName} updateAndFlush(${d_tableName} ${x_tableName}){
    	${d_tableName} ${x_tableName}_ = null;
    	ReentrantLock lock = SK_Lock.getLock(lOCK_KEY);
    	if (lock.isHeldByCurrentThread())
    		${x_tableName}_ = super.updateAndFlush(${x_tableName});
    	unlock();
    	return ${x_tableName}_;
    }
    
    
    public boolean deleteAndFlush(${d_tableName} ${x_tableName}){
    	boolean bool = false;
    	ReentrantLock lock = SK_Lock.getLock(lOCK_KEY);
    	if (lock.isHeldByCurrentThread()) 
    		return super.deleteAndFlush(${x_tableName});
    	unlock();
    	return bool;
    }
}